package com.aube.plugin.episode;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.aube.plugin.origin.R;
import com.huyn.baseframework.dynamicload.DLLayout;
import com.huyn.baseframework.dynamicload.DynamicLoaderHelper;
import com.huyn.baseframework.dynamicload.IPositionListener;
import com.huyn.baseframework.dynamicload.IPositionManager;
import com.huyn.baseframework.model.Category;
import com.huyn.baseframework.utils.SysUtil;
import com.huyn.baseframework.utils.UmengUtils;
import com.huyn.baseframework.utils.Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by huyaonan on 16/10/17.
 */
public class VideoGrid extends DLLayout {

    private List<VideoItem> mAnimItems = new ArrayList<>();
    private AtomicBoolean mAnimated = new AtomicBoolean(false);
    private int itemWidth, space;
    private Category mCategory;

    private LinearLayout mLayoutTop, mLayoutBottom;
    private View[] mGuideTarget;

    public VideoGrid(Context context) {
        super(context);
    }

    public VideoGrid(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public VideoGrid(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void initViews() {
        inflate(R.layout.video_grid, this, true);

        mLayoutTop = (LinearLayout) findViewById(R.id.video_grid_top);
        mLayoutBottom = (LinearLayout) findViewById(R.id.video_grid_bottom);
    }

    @Override
    public void fillData(Object o) {
    }

    public void initData(List<VideoModel> list, Category category, IUmengListener listener) {
        if(list == null || list.size() == 0)
            return;
        space = getResources().getDimensionPixelOffset(R.dimen.widget_margin);

        mCategory = category;
        int width = SysUtil.getAbsoluteScreenWidth(context);
        itemWidth = (width-4*space)/2;
        int size = list.size();
        mGuideTarget = new View[2];
//        mGuideTarget[0] = fillVideo(list.get(0), mLayoutTop, listener, true).getImageView();
//        if(size > 1)
//            mGuideTarget[1] = fillVideo(list.get(1), mLayoutTop, listener, false).getImageView();
        fillVideo(list.get(0), mLayoutTop, listener, true).getImageView();
        if(size > 1)
            fillVideo(list.get(1), mLayoutTop, listener, false).getImageView();
        if(size > 2)
            fillVideo(list.get(2), mLayoutBottom, listener, true);
        if(size > 3)
            fillVideo(list.get(3), mLayoutBottom, listener, false);

        if(size >= 3) {
            mGuideTarget[0] = mAnimItems.get(2).getImageView();
            mGuideTarget[1] = mAnimItems.get(2).getImageView();
            return;
        }
        mGuideTarget[0] = mAnimItems.get(0).getImageView();
        mGuideTarget[1] = mAnimItems.get(0).getImageView();
    }

    private VideoItem fillVideo(final VideoModel video, LinearLayout parent, final IUmengListener listener, boolean addGap) {
        VideoItem mVideo = (VideoItem) DynamicLoaderHelper.loadTargetLayout(context, getDLPluginPackage(), VideoItem.class.getName());
        parent.addView(mVideo, new LinearLayout.LayoutParams(itemWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
        mVideo.setupContent(video, itemWidth);

        if(addGap) {
            parent.addView(new View(context), new LinearLayout.LayoutParams(space, itemWidth * 3 / 4));
        }

        mVideo.setAlpha(0f);
        mAnimItems.add(mVideo);

        if (video.isLegal()) {
            mVideo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mBridge != null)
                        mBridge.onClickToVideo(video.toChannelDetail(), mCategory);
                    if (listener != null)
                        listener.reportToUmeng(UmengUtils.appendLabels(video.videoid, video.videoTitle));
                }
            });
        }

        return mVideo;
    }

    @Override
    public void notifyAnim(int i) {
        if(mAnimated.get())
            return;
        mAnimated.set(true);
        if(mAnimItems == null)
            return;
        notifyItemAnim(i + 300);
    }

    private void notifyItemAnim(int animDelay) {
        int i=0;
        for(IPositionListener item : mAnimItems) {
            item.notifyAnim(animDelay + i*200);
            i++;
        }
    }

    @Override
    public void registToManager(IPositionManager iPositionManager) {

    }

    @Override
    public void unreistFromManager(IPositionManager iPositionManager) {

    }

    @Override
    public int[] getLocations() {
        return new int[0];
    }

    public View[] getGuideTarget() {
        return mGuideTarget;
    }

}
